﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.项目成果管理.service;
using DXApplicationTangche.UC.工序管理.工序详情;
using DXApplicationTangche.UC.项目成果管理.在制品管理;
using DXApplicationTangche.UC.面料库存;
using DevExpress.XtraGrid.Demos.util;

namespace DXApplicationTangche.UC.项目成果管理
{
    public partial class Form项目成果物管理 : DevExpress.XtraEditors.XtraForm
    {
        private DTO工序管理 DTO工序管理 = new DTO工序管理();
        private wbsItemDTO wbsItemDTO = new wbsItemDTO();
        private String code;
        private String ID;
        private Boolean 是否新增 = false;
        public Form项目成果物管理()
        {
            InitializeComponent();
            try
            {
                this.DTO工序管理 = 项目成果Service.get全部工序("SSHIRT");
                this.treeList项目.DataSource = this.DTO工序管理.wbsItemDTOList;
                this.treeList项目.ExpandAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        private void treeList项目_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            this.ID = this.treeList项目.FocusedNode["id"].ToString();
            this.code = this.treeList项目.FocusedNode["code"].ToString();
            foreach (wbsItemDTO item in this.DTO工序管理.wbsItemDTOList)
            {
                if (this.ID.Equals(item.id))
                {
                    this.wbsItemDTO = item;
                    break;
                }
            }
            //this.wbsItemDTO = 项目成果Service.get点击单个项目(this.code);
            this.textBoxID.Text = wbsItemDTO.id;
            this.textBoxParentId.Text = wbsItemDTO.parentId;
            this.textBox名称.Text = wbsItemDTO.activityDescription;
            this.textBox编码.Text = wbsItemDTO.code;
            this.textBox顺序.Text = wbsItemDTO.index;
            this.完成设置();
        }
        private void simpleButton删除_Click(object sender, EventArgs e)
        {
            this.DTO工序管理 = 项目成果Service.get全部工序("SSHIRT");
            this.treeList项目.DataSource = this.DTO工序管理.wbsItemDTOList;
            this.treeList项目.ExpandAll();
        }

        private void simpleButton新增_Click(object sender, EventArgs e)
        {
            this.是否新增 = true;
            this.新增设置();
            this.simpleButton.Enabled = true;
            this.simpleButton修改.Enabled = false;
            this.simpleButton删除.Enabled = false;
        }

        private void simpleButton修改_Click(object sender, EventArgs e)
        {
            this.textBoxID.ReadOnly = true;
            this.textBoxParentId.ReadOnly = true;
            this.textBox名称.ReadOnly = false;
            this.textBox编码.ReadOnly = false;
            this.textBox顺序.ReadOnly = false;
            this.simpleButton.Enabled = true;
        }

        private void simpleButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBoxID.Text.Trim()) 
                || String.IsNullOrWhiteSpace(this.textBox名称.Text.Trim())
                || String.IsNullOrWhiteSpace(this.textBox编码.Text.Trim())
                || String.IsNullOrWhiteSpace(this.textBox顺序.Text.Trim()))
            {
                MessageBox.Show("请将信息填写完整");
                return;
            }
            if (是否新增)
            {
                this.DTO工序管理.wbsItemDTOList.Add(
                    new wbsItemDTO(
                        this.textBoxID.Text.Trim(),
                        this.textBoxParentId.Text.Trim(),
                        this.textBox名称.Text.Trim(),
                        this.textBox编码.Text.Trim(),
                        this.textBox顺序.Text.Trim()
                        )
                    );
            }
            else
            {
                foreach (wbsItemDTO item in this.DTO工序管理.wbsItemDTOList)
                {
                    if (this.ID.Equals(item.id))
                    {
                        item.activityDescription = this.textBox名称.Text.Trim();
                        item.code = this.textBox编码.Text.Trim();
                        item.index = this.textBox顺序.Text.Trim();
                        break;
                    }
                }
            }
            try
            {
                项目成果Service.post工序(this.DTO工序管理);
                MessageBox.Show("操作完成");

            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
            this.完成设置();
            this.DTO工序管理 = 项目成果Service.get全部工序("SSHIRT");
            this.treeList项目.DataSource = this.DTO工序管理.wbsItemDTOList;
            this.treeList项目.ExpandAll();
        }
        private void 新增设置()
        {
            this.textBoxID.Clear();
            this.textBoxParentId.Clear();
            this.textBox名称.Clear();
            this.textBox编码.Clear();
            this.textBox顺序.Clear();
            this.textBoxID.ReadOnly = false; ;
            this.textBoxParentId.ReadOnly = false;
            this.textBox名称.ReadOnly = false;
            this.textBox编码.ReadOnly = false;
            this.textBox顺序.ReadOnly = false;
            this.simpleButton修改.Enabled = false;
            this.simpleButton删除.Enabled = false;
        }
        private void 完成设置()
        {
            this.textBoxID.ReadOnly = true; ;
            this.textBoxParentId.ReadOnly = true;
            this.textBox名称.ReadOnly = true;
            this.textBox编码.ReadOnly = true;
            this.textBox顺序.ReadOnly = true;
            this.simpleButton.Enabled = false;
            this.simpleButton新增.Enabled = true;
            this.simpleButton修改.Enabled = true;
            this.simpleButton删除.Enabled = true;
        }

        //private void treeList项目_DoubleClick(object sender, EventArgs e)
        //{
        //    this.code = this.treeList项目.FocusedNode["code"].ToString();
        //    this.wbsItemDTO = 项目成果Service.get点击单个项目(this.code);
        //    if ("".Equals(this.wbsItemDTO.parentId))
        //    {
        //        return;
        //    }
        //    //领料出库审核
        //    else if ("7".Equals(this.wbsItemDTO.id))//面料审核
        //    {
        //        new Form订单领料出库(this.code).ShowDialog();
        //    }
        //    //领料出库
        //    //else if ("10".Equals(this.wbsItemDTO.id))//面料出库
        //    //{
        //    //    new Form投料详情(this.code).ShowDialog();
        //    //}
        //    //在制品管理
        //    else if ("10".Equals(this.wbsItemDTO.id))//裁剪
        //    {
        //        new Form在制品仅记录(this.code).ShowDialog();
        //    }
        //    else if ("14".Equals(this.wbsItemDTO.id))//案板
        //    {
        //        new Form在制品仅记录(this.code).ShowDialog();
        //    }
        //    else if ("15".Equals(this.wbsItemDTO.id))//机台
        //    {
        //        new Form在制品仅记录(this.code).ShowDialog();
        //    }
        //    else if ("16".Equals(this.wbsItemDTO.id))//刺绣
        //    {
        //        new Form在制品仅记录(this.code).ShowDialog();
        //    }
        //    else if ("17".Equals(this.wbsItemDTO.id)) //VI质检
        //    {
        //        new FormVI质检(this.code).ShowDialog();
        //    }
        //    else if ("18".Equals(this.wbsItemDTO.id))//钉锁
        //    {
        //        new Form在制品仅记录(this.code).ShowDialog();
        //    }
        //    else if ("19".Equals(this.wbsItemDTO.id))//成品质检
        //    {
        //        new FormVI质检(this.code).ShowDialog();
        //    }
        //    //产成品管理
        //}
    }
}